package com.ruoyi.store.service.impl;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.store.domain.StoGoods;
import com.ruoyi.store.domain.StoOrders;
import com.ruoyi.store.domain.StoOrdersDTO;
import com.ruoyi.store.mapper.StoOrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.store.mapper.StoOrderDetailMapper;
import com.ruoyi.store.domain.StoOrderDetail;
import com.ruoyi.store.service.IStoOrderDetailService;

/**
 * 订单明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-08
 */
@Service
public class StoOrderDetailServiceImpl implements IStoOrderDetailService 
{
    @Autowired
    private StoOrderDetailMapper stoOrderDetailMapper;

    @Autowired
    private StoOrdersMapper stoOrdersMapper;


    @Override
    public int addStoOrder(StoOrdersDTO stoOrdersDTO) {
        // 打印 form 的内容
        System.out.println("form: " + stoOrdersDTO.getForm());


        StoOrders stoOrders = new StoOrders();
        BeanUtils.copyBeanProp(stoOrders, stoOrdersDTO.getForm());

        stoOrders.setOrderTime(DateUtils.getNowDate());
        stoOrders.setNumber(DateUtils.dateTimeNow());
        // 获取商品列表
        List<StoGoods> goodsList = stoOrdersDTO.getGoods();
        if (goodsList != null && !goodsList.isEmpty()) {
            StoGoods firstGoods = goodsList.get(0);
            System.out.println("firstGoods: " + firstGoods);
            stoOrders.setImage(firstGoods.getImage());
            System.out.println("firstGoods.getImage(): " + stoOrders.getImage());
            System.out.println("sto: "+stoOrders);
        }
        stoOrdersMapper.insertStoOrders(stoOrders);



        //插入订单明细
        stoOrdersDTO.getGoods().forEach(goods -> {
            StoOrderDetail stoOrderDetail = new StoOrderDetail();
            stoOrderDetail.setName(goods.getName());
            stoOrderDetail.setImage(goods.getImage());
            stoOrderDetail.setOrderId(stoOrders.getId());
            stoOrderDetail.setDishId(goods.getDishId());
            stoOrderDetail.setSetmealId(goods.getSetmealId());
            stoOrderDetail.setDishFlavor(goods.getDishFlavor());
            stoOrderDetail.setNumber(goods.getNumber());
            stoOrderDetail.setAmount(goods.getAmount());
            stoOrderDetailMapper.insertStoOrderDetail(stoOrderDetail);
        });
        return 200;
    }
    /**
     * 查询订单明细
     * 
     * @param id 订单明细主键
     * @return 订单明细
     */
    @Override
    public StoOrderDetail selectStoOrderDetailById(Long id)
    {
        return stoOrderDetailMapper.selectStoOrderDetailById(id);
    }

    /**
     * 查询订单明细列表
     * 
     * @param stoOrderDetail 订单明细
     * @return 订单明细
     */
    @Override
    public List<StoOrderDetail> selectStoOrderDetailList(StoOrderDetail stoOrderDetail)
    {
        return stoOrderDetailMapper.selectStoOrderDetailList(stoOrderDetail);
    }

    /**
     * 新增订单明细
     * 
     * @param stoOrderDetail 订单明细
     * @return 结果
     */
    @Override
    public int insertStoOrderDetail(StoOrderDetail stoOrderDetail)
    {
        return stoOrderDetailMapper.insertStoOrderDetail(stoOrderDetail);
    }

    /**
     * 修改订单明细
     * 
     * @param stoOrderDetail 订单明细
     * @return 结果
     */
    @Override
    public int updateStoOrderDetail(StoOrderDetail stoOrderDetail)
    {
        return stoOrderDetailMapper.updateStoOrderDetail(stoOrderDetail);
    }

    /**
     * 批量删除订单明细
     * 
     * @param ids 需要删除的订单明细主键
     * @return 结果
     */
    @Override
    public int deleteStoOrderDetailByIds(Long[] ids)
    {
        return stoOrderDetailMapper.deleteStoOrderDetailByIds(ids);
    }

    /**
     * 删除订单明细信息
     * 
     * @param id 订单明细主键
     * @return 结果
     */
    @Override
    public int deleteStoOrderDetailById(Long id)
    {
        return stoOrderDetailMapper.deleteStoOrderDetailById(id);
    }


}
